﻿using System;
using NHibernate;
using YunBox2.Contracts;
using YunBox2.Contracts.Core;
using YunBox2.DAL;
using YunBox2.Server.Core;

namespace YunBox2.Server.Services
{
    public partial class BoxService : ISystemService
    {
        #region ISystemService 成员
        public bool Login(string userName, string password)
        {
            var clientContext = GetMySessionContext();

            var session = DbHelper.GetCurrentSession();

            var user = session.QueryOver<User>().Where(x => x.UserName == userName && x.Password == password).SingleOrDefault();
            if(user!=null)
            {
                user.LastLoginTime = DateTime.Now;
                session.Update(user);
                session.Flush();
                clientContext.CurrentUser = user;
                return true;
            }

            return false;
        }

        public User GetMyProfile()
        {
            return GetMySessionContext().CurrentUser;
        }


        public bool Logoff()
        {
            var clientContext = GetMySessionContext();
            if (clientContext == null)
            {
                return false;
            }
            if(clientContext.CurrentUser==null)
            {
                return false;
            }
            clientContext.CurrentUser.LastLogoutTime = DateTime.Now;

            ISession session = DbHelper.GetCurrentSession();
            session.SaveOrUpdate(clientContext.CurrentUser);
            session.Flush();
            session.Close();

            clientContext.CurrentUser = null;

            return true;
        }


        public bool ChangeMyPassword(string oldPassword, string NewPassword)
        {
            User user = GetMySessionContext().CurrentUser;
            if (user == null)
            {
                return false;
            }
            if (oldPassword != user.Password)
            {
                return false;
            }
            user.Password = NewPassword;
            user.LastChangedPassword = DateTime.Now;
            ISession session = DbHelper.CreateSession();
            session.SaveOrUpdate(user);
            session.Flush();
            session.Close();
            return true;
        }

        public bool ChangeMyEmail(string email)
        {
            User user = GetMySessionContext().CurrentUser;
            if (user == null)
            {
                return false;
            }
            user.Email=email;
            user.LastModifiedTime = DateTime.Now;
            ISession session = DbHelper.GetCurrentSession();
            session.SaveOrUpdate(user);
            session.Flush();
            session.Close();
            return true;
        }

        #region 公司信息管理

        public bool CompanyProfileToUpdate(Guid clientID, CompanyProfile companyProfile)
        {
            User user = GetMyProfile();
            if(user==null)
            {
                return false;
            }

            ISession session = DbHelper.GetCurrentSession();
            try
            {
                CompanyProfile c = session.QueryOver<CompanyProfile>().SingleOrDefault();
                if (c == null)
                {
                    if (companyProfile.Id.HasValue)
                    {
                        if (companyProfile.Id.Value == Guid.Empty)
                        {
                            companyProfile.Id = Guid.NewGuid();
                        }
                    }
                    companyProfile.LastModifiedTime = DateTime.Now;
                    companyProfile.LastModifiedUser = user;

                    session.Save(companyProfile);
                }
                else
                {
                    c.Name = companyProfile.Name;
                    c.ShortName = companyProfile.ShortName;
                    c.LawPerson = companyProfile.LawPerson;
                    c.BankAccount = companyProfile.BankAccount;
                    c.Bank = companyProfile.Bank;

                    c.LastModifiedTime = DateTime.Now;
                    c.LastModifiedUser = user;

                    session.SaveOrUpdate(c);
                }
                session.Flush();
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                session.Close();
            }
        }

        public CompanyProfile CompanyProfileToGetProfile(Guid clientID)
        {
            ISession session = DbHelper.CreateSession();
            return session.QueryOver<CompanyProfile>().SingleOrDefault();
        }

        #endregion

        #endregion

      
    }
}